package com.baidu.process.config;


import okhttp3.MediaType;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Map;

public class HttpUtil {
    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    public static String sendHttpPost(String postUrl, String paramJson, Map<String,String> headers) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost= new HttpPost(postUrl);
        for (String key : headers.keySet()){
            httpPost.setHeader(key,headers.get(key));
        }
        httpPost.setEntity(new StringEntity(paramJson));
        for (Header header: httpPost.getAllHeaders()){
            System.out.println(header.toString());
        }
        CloseableHttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        String result = EntityUtils.toString(entity,"UTF-8");
        response.close();
        httpClient.close();
        return result;
    }

}
